<template>
  <div class="scan_record">
    <div class="tool select">

      <el-form :inline="true">
        <el-form-item>
          <ToolBar :update-data="updateById"></ToolBar>
        </el-form-item>
        <el-form-item label="姓名：">
          <el-select v-model="form.userIds" placeholder="请选择员工姓名..." value-key="id" @change="handleCurrentChange(1)" size="small" multiple :collapse-tags="true" style="width: 20vw;"
                     filterable clearable>
            <el-option :label="item.userName+'（'+item.jobNo+'）'" :value="item.userId" v-for="item in $store.state.userList">
              <span style="float: left">{{ item.userName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.jobNo }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="出勤日期：">
          <el-date-picker size="small"
                          v-model="attendanceDate"
                          type="daterange"
                          align="left"
                          unlink-panels
                          range-separator="至"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          :default-time="['00:00:00', '23:59:59']"
                          :picker-options="pickerOptions"
                          :clearable="false"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>


          <el-button size="small" type="primary" @click="handleCurrentChange(1)">筛选
          </el-button>
          <el-button size="small" type="primary" @click="goSearch"
                     icon="el-icon-search">搜索
          </el-button>
          <el-button size="small" type="primary" @click="reSet"
                     icon="el-icon-refresh">重置
          </el-button>
          <el-button size="small" type="primary" @click="goGetData"
                     icon="el-icon-download">获取生产排班数据
          </el-button>
        </el-form-item>
      </el-form>

    </div>
    <el-dialog title="生产排班数据" :visible.sync="dialogFormVisible" center>
      <el-row>
        <el-form :model="form">
          <el-col :span="12">
            <el-form-item label="入职时间" :label-width="formLabelWidth">
              <el-date-picker
                  class="w100" size="small"
                  v-model="form.hiredDate"
                  align="right"
                  type="date"
                  placeholder="选择日期"
                  :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="档案时间" :label-width="formLabelWidth">
              <el-date-picker
                  class="w100" size="small"
                  v-model="form.archivesDate"
                  align="right"
                  type="date"
                  placeholder="选择日期"
                  :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工姓名" :label-width="formLabelWidth">
              <el-select v-model="form.userId" placeholder="请选择员工姓名..." size="small" value-key="id" @change="handleUserChange"
                         filterable clearable class="w100">
                <el-option :label="item.userName+'（'+item.jobNo+'）'" :value="item.userId" v-for="item in finUserRosterList" :key="item.id">
                  <span style="float: left">{{ item.userName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.jobNo }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考勤结果" :label-width="formLabelWidth">
              <el-select v-model="form.timeResult" placeholder="请选择考勤结果..."size="small" value-key="id" filterable clearable
                         class="w100">
                <el-option :label="item.label" :value="item.value" v-for="item in timeResultList" :key="item.id">
                  <span style="float: left">{{ item.label }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="getDataList" size="small">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="生产排班数据获取" :visible.sync="getDataDialogFormVisible" center>

      <el-form :model="form">
        <el-row>
          <el-col :span="24">
            <el-date-picker style="width: 100%!important;" size="small"
                            v-model="attendanceDate"
                            type="daterange"
                            align="left"
                            unlink-panels
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            :default-time="['00:00:00', '23:59:59']"
                            :picker-options="pickerOptions">
            </el-date-picker>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="getDataDialogFormVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="getDataFromDingDing" size="small">确 定</el-button>
      </div>
    </el-dialog>


    <el-table
        :data="tableData"
        stripe
        border
        default-expand-all
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}">
      <el-table-column type="selection" width="50" label="序号">
      </el-table-column>
      <el-table-column label="考勤日期" prop="workDate" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.workDate|formatDateBirth }}
        </template>
      </el-table-column>
      <el-table-column label="星期">
        <template slot-scope="scope">
          {{ scope.row.workDate|formatDateDDDD }}
        </template>
      </el-table-column>
      <el-table-column label="员工姓名" prop="userName">
      </el-table-column>
      <el-table-column label="员工工号" prop="jobNo">
      </el-table-column>
      <el-table-column label="部门" min-width="150">
        <template slot-scope="scope">
          {{scope.row.deptLevel1}}-{{scope.row.deptLevel2}}-{{scope.row.deptLevel3}}
        </template>
      </el-table-column>
      <el-table-column label="班别" prop="shift">
      </el-table-column>
      <el-table-column label="是否休息" prop="isRest">
      </el-table-column>
      <el-table-column label="基准打卡时间">
        <template slot-scope="scope">
          {{ scope.row.planCheckTime|formatDate }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.createTime|formatDate }}
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          hide-on-single-page
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[size, size*2, size*3, total]"
          :page-size=size
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
  </div>
</template>

<script>
import ToolBar from "@/components/common/ToolBar";

export default {
  name: "ProductRostering",
  components: {ToolBar},
  data() {
    return {
      currentPage: 1,
      total: 0,
      size: 12,
      tableData: [],
      dingUsersListName: null,
      finUserRosterList: [],
      userList: [],
      attendanceDate: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      dialogFormVisible: false,
      getDataDialogFormVisible: false,
      form: {
        start: null,
        end: null,
        userId: null,
        name: null,
        workDate: new Date().getTime() - 3600 * 1000 * 24,
        timeResult: null,
        checkType: null,
      },
      formLabelWidth: '120px',
      timeResultList: [
        {
          label: "正常",
          value: "Normal",
        },
        {
          label: "早退",
          value: "Early",
        }, {
          label: "迟到",
          value: "Late",
        }, {
          label: "严重迟到",
          value: "SeriousLate",
        }, {
          label: "旷工迟到",
          value: "Absenteeism",
        }, {
          label: "未打卡",
          value: "NotSigned",
        },
      ]
    }
  },
  created() {
    this.attendanceDate[0] = this.$tools.getCurrWeekDays().start
    this.attendanceDate[1] = this.$tools.getCurrWeekDays().end
    this.getDataList()

  },
  methods: {
    tableRowClassName({row, rowIndex}) {
      console.log(row.basicattendance)
      if (row.basicattendance < 8) {
        return 'error-row';
      } else if (row.basicattendance === 8) {
        return 'success-row';
      }
      return '';
    },
    goGetData() {
      this.getDataDialogFormVisible = true
    },
    getDataList() {
      this.dialogFormVisible = false
      this.form.start = this.attendanceDate[0]
      this.form.end = this.attendanceDate[1]
      console.log("查询数据：", this.form)
      axios.post("/ding_product_rostering/list/" + this.currentPage + "/" + this.size, this.form).then(res => {
        console.log("打卡信息：", res)
        if (res.data.success) {
          this.tableData = res.data.data.records
          this.total = res.data.data.total
          this.pages = res.data.data.pages
        } else {
          this.tableData = []
          this.total = 0
          this.pages = 1

        }

      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this.getDataList()
    },
    goSearch() {
      this.dialogFormVisible = true
    },
    reSet() {
      this.form = {
        start: null,
        end: null,
        userId: null,
        name: null,
        workDate: null,
        timeResult: null,
        checkType: null,
      }
      this.getDataList()

    },
    getUserList() {
      axios.get("/fin_user_roster/list").then(res => {
        console.log("用户：", res.data.data)
        this.finUserRosterList = res.data.data
      })
    },
    handleChange() {
      // this.$store.commit("setUser", this.form.productline)

      this.getDataList()
    },
    handleUserChange() {
      // this.form.userId=
      console.log('Form:', this.form)
    },
    deleteById() {
      console.log("用户扫描删除")
    },
    updateById() {
      console.log("用户扫描修改")
    },
    getDataFromDingDing() {
      this.form.start = this.attendanceDate[0]
      this.form.end = this.attendanceDate[1]
      console.log("查询数据：", this.form)
      this.$tools.openLoading()
      axios.post("/ding_product_rostering/get", this.form).then(res => {
        if (res.data.success) {
          this.getDataDialogFormVisible = false
          this.getDataList()
          this.$tools.closeLoading()

        }
      })
    }
  }

}
</script>


<style scoped lang="less">
.scan_record {
  .select {
    //margin-bottom: 0.5rem;
    display: inline-flex !important;

    .el-form-item {
      margin-bottom: 0 !important;
    }
  }
}

</style>
